package com.voxel.sense.infomsg.module.publish.details;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.com.common.app.BViewActivity;
import com.example.com.common.utils.ListUtil;
import com.example.com.common.view.adapter.TBaseAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.utils.GlideUtils;
import com.voxel.sense.infomsg.widget.CommonImagePagerActivity;
import com.voxel.sense.infomsg.widget.adapter.FileGriviewAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.constants.IntentConstants;
import cyy.example.com.facotry.model.api.MyPublishListEntity;
import cyy.example.com.facotry.share.SPEngine;

/**
 * Created by ChenYiYao on 2018/10/22.
 * des : 发布详情
 */
public class PublishDetActivity extends BViewActivity<PublishDetContract.Presenter>
        implements PublishDetContract.View {
    @BindView(R.id.iv_portrait)
    ImageView iv_portrait; //头像
    @BindView(R.id.tv_name)
    TextView tv_name; //发布人
    @BindView(R.id.tv_time)
    TextView tv_time; //发布时间
    @BindView(R.id.tv_firm_name)
    TextView tv_firm_name;
    @BindView(R.id.tv_address)
    TextView tv_address; //公司地址
    @BindView(R.id.tv_advice)
    TextView tv_advice;//反馈意见
    @BindView(R.id.tv_context_text)
    TextView tv_context_text;//反馈意见描述文字
    @BindView(R.id.tv_pic_tip_desc)
    TextView tv_pic_tip_desc;//反馈意见描述文字
    @BindView(R.id.gridview_image)
    GridView gridview_image;

    private TBaseAdapter mImageAdapter; //文件展示的适配器

    public static void show(MyPublishListEntity data, Context context) {
        Intent intent = new Intent(context, PublishDetActivity.class);
        intent.putExtra(IntentConstants.OBJECT_TYPE, data);
        context.startActivity(intent);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_publish_det;
    }

    @Override
    protected PublishDetContract.Presenter initPresenter() {
        return new PublishDetPresenter(this);
    }


    @Override
    protected boolean initArgs(Bundle bundle) {
        return bundle != null && bundle.getSerializable(IntentConstants.OBJECT_TYPE) instanceof MyPublishListEntity;
    }

    @Override
    protected void initData() {
        super.initData();
        MyPublishListEntity entity = (MyPublishListEntity) getIntent().getSerializableExtra(IntentConstants.OBJECT_TYPE);
        tv_name.setText(entity.getCreateName());
        GlideUtils.GlideLoadPortrait(Factory.app(), SPEngine.getSPEngine().getUserInfo().getPortrait(), iv_portrait);
        tv_time.setText(entity.getCreateTime());
        tv_firm_name.setText(entity.getCompanyName());
        tv_address.setText(entity.getDetailAddr());
        mPresenter.doLoad(entity.getRecId());

        mImageAdapter = new FileGriviewAdapter(this, Constants.file_iage);
        gridview_image.setAdapter(mImageAdapter);
    }

    @Override
    protected void initEven() {
        super.initEven();
        gridview_image.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //查看图片
                CommonImagePagerActivity.showImages(PublishDetActivity.this, (ArrayList<String>) mImageAdapter.getList(), position);
            }
        });
    }

    @Override
    public void onLoadDetails(String desc, List<String> files) {
        if (!TextUtils.isEmpty(desc)) {
            tv_context_text.setVisibility(View.VISIBLE);
            tv_advice.setVisibility(View.VISIBLE);
            tv_advice.setText(desc);
        }

        if (!ListUtil.isEmpty(files)) {
            tv_pic_tip_desc.setVisibility(View.VISIBLE);
            gridview_image.setVisibility(View.VISIBLE);
            mImageAdapter.setList(files);
        }
    }

}
